﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Dynamic;
using System.Data.SqlClient;

namespace Trade.DBUtility
{
    public class ExtendObject
    {
        public static ExpandoObject AutoMap(DataRow drv)
        {
            var item = new ExpandoObject();

            var itemDictionary = (IDictionary<string, object>)item;

            foreach (DataColumn column in drv.Table.Columns)
            {
                if (drv[column.ColumnName] == DBNull.Value)
                    itemDictionary.Add(column.ColumnName, null);
                else
                {

                    if (column.DataType.Name == "Decimal")
                    {
                        itemDictionary.Add(column.ColumnName, drv[column.ColumnName]);
                    }
                    else
                    {
                        itemDictionary.Add(column.ColumnName, drv[column.ColumnName].ToString());
                    }
                }
            }
            return item;
        }

    }
}
